﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;

namespace Common
{
    public class UtilityFuncation
    {
        private static string[] SpecialChar = new string[] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "∴", "∵", ":", "∷", "!", "、", "^", "*", "-", "=", "。", "；", "-", "←", "↑", "→", "↓", "↔", "↕", "↖", "↗", "↘", "↙", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "－", "×", "÷", "+", "－", "±", "/", "∫" };//83
        private static string[] EmojiArr = new string[] { "[Smile]", "[CoolGuy]", "[Drool]", "[Scowl]", "[Shy]", "[Tongue]", "[Grin]", "[Surprise]", "[Ruthless]", "[Blush]", "[Chuckle]", "[Joyful]", "[Slight]", "[Smug]", "[Laugh]", "[Commando]", "[Determined]", "[Shocked]", "[Shhh]", "[Toasted]", "[NosePick]", "[Clap]", "[Shame]", "[Trick]", "[Sly]", "[Kiss]", "[Whimper]", "[Watermelon]", "[Beer]", "[Coffee]", "[Rose]", "[Heart]", "[Lips]"};//33个
        /// <summary>
        /// 将指定字符串添加特殊标识，实现语句唯一
        /// </summary>
        /// <param name="str">待处理字符串</param>
        /// <param name="IsAddChar">是否添加特殊字符</param>
        /// <param name="IsAddEmoji">是否添加表情</param>
        /// <param name="IsAddTrim">是否添加空格</param>
        /// <returns></returns>
        public static string ConvertUniqueString(string str, bool IsAddChar, bool IsAddEmoji,bool IsAddTrim)
        {
            Random random = new Random();
            if (IsAddChar)
            {
                str = SpecialChar[random.Next(SpecialChar.Length)] + str;
            }
            if (IsAddTrim)
            {
                int randone = random.Next(str.Length);
                str = str.Substring(0,randone) + " " + str.Substring(randone);
            }
            if (IsAddEmoji)
            {
                str += EmojiArr[random.Next(EmojiArr.Length)];
            }
            return str;
        }
        /// <summary>
        /// 删除最后结尾的指定字符后的字符
        /// </summary>
        public static string DelLastChar(string str, string strchar)
        {
            if (string.IsNullOrEmpty(str))
                return "";
            if (str.LastIndexOf(strchar) >= 0 && str.LastIndexOf(strchar) == str.Length - 1)
            {
                return str.Substring(0, str.LastIndexOf(strchar));
            }
            return str;
        }
        /// <summary>
        /// 读cookie值
        /// </summary>
        /// <param name="strName">名称</param>
        /// <returns>cookie值</returns>
        public static string GetCookie(string strName, string key)
        {
            if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[strName] != null && HttpContext.Current.Request.Cookies[strName][key] != null)
                return UrlDecode(HttpContext.Current.Request.Cookies[strName][key].ToString());

            return "";
        }
        /// <summary>
        /// URL字符编码
        /// </summary>
        public static string UrlEncode(string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return "";
            }
            str = str.Replace("'", "");
            return HttpContext.Current.Server.UrlEncode(str);
        }

        /// <summary>
        /// URL字符解码
        /// </summary>
        public static string UrlDecode(string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return "";
            }
            return HttpContext.Current.Server.UrlDecode(str);
        }
        /// <summary>
        /// 组合URL参数
        /// </summary>
        /// <param name="_url">页面地址</param>
        /// <param name="_keys">参数名称</param>
        /// <param name="_values">参数值</param>
        /// <returns>String</returns>
        public static string CombUrlTxt(string _url, string _keys, params string[] _values)
        {
            StringBuilder urlParams = new StringBuilder();
            try
            {
                string[] keyArr = _keys.Split(new char[] { '&' });
                for (int i = 0; i < keyArr.Length; i++)
                {
                    if (!string.IsNullOrEmpty(_values[i]))
                    {
                        _values[i] = UrlEncode(_values[i]);
                        urlParams.Append(string.Format(keyArr[i], _values) + "&");
                    }
                }
                if (!string.IsNullOrEmpty(urlParams.ToString()) && _url.IndexOf("?") == -1)
                    urlParams.Insert(0, "?");
            }
            catch
            {
                return _url;
            }
            return _url + DelLastChar(urlParams.ToString(), "&");
        }

        /// <summary>  
        /// 取得某月的第一天  
        /// </summary>  
        /// <param name="datetime">要取得月份第一天的时间</param>  
        /// <returns></returns>  
        public static DateTime FirstDayOfMonth(DateTime datetime)
        {
            return datetime.AddDays(1 - datetime.Day);
        }

        /// <summary>  
        /// 取得某月的最后一天  
        /// </summary>  
        /// <param name="datetime">要取得月份最后一天的时间</param>  
        /// <returns></returns>  
        public static DateTime LastDayOfMonth(DateTime datetime)
        {
            return datetime.AddDays(1 - datetime.Day).AddMonths(1).AddDays(-1);

        }

        /// <summary>  
        /// 取得本周1的日期
        /// </summary>  
        /// <param name="datetime">要取得月份第一天的时间</param>  
        /// <returns></returns>  
        public static DateTime FirstDayOfWeeK(DateTime datetime)
        {
            int dayOfWeek = -1 * (int)datetime.Date.DayOfWeek;
            //Sunday = 0,Monday = 1,Tuesday = 2,Wednesday = 3,Thursday = 4,Friday = 5,Saturday = 6,
            DateTime weekStartTime = datetime.AddDays(dayOfWeek + 1);//取本周一
            if (dayOfWeek == 0) //如果今天是周日，则开始时间是上周一
            {
                weekStartTime = weekStartTime.AddDays(-7);
            }
            return weekStartTime.Date;
        }

        /// <summary>
        /// 验证手机号码是否合法
        /// </summary>
        /// <param name="phone"></param>
        /// <returns></returns>
        public static bool IsPhoneNumber(string phone)
        {
            return System.Text.RegularExpressions.Regex.IsMatch(phone, @"^1[3456789]\d{9}$");
        }

        /// <summary>
        /// HTTP GET方式请求数据.
        /// </summary>
        /// <param name="url">URL.</param>
        /// <returns></returns>
        public static string HttpGet(string url)
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
            request.Method = "GET";
            request.Accept = "*/*";
            WebResponse response = null;
            string responseStr = null;

            try
            {
                response = request.GetResponse();

                if (response != null)
                {
                    StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                    responseStr = reader.ReadToEnd();
                    reader.Close();
                }
            }
            catch (Exception e)
            {
                return e.Message;
            }
            finally
            {
                request = null;
                response = null;
            }
            return responseStr;
        }
    }
}